/**
 * Description:
 *
 * @author      Eden
 * @datetime    2016/12/10 23:26
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */
(function () {

    var api = {
        getSliderDetail: '/bbs/slider/detail',
        getBaseList: '/farm/base/api',
        getProductList: '/goods/lists/api',
        postData: '/bbs/slider/editSlider',
        deleteData: '/bbs/slider/deleteSlider',
        changeStatus: '/bbs/slider/chgStatus'
    };
    sm.controller('admin-bbs-slider-edit-controller', ['$scope', '$http', 'Modal', function ($scope, $http, Modal) {

        $scope.type = 'link';
        $scope.title = '';
        $scope.source = '';
        $scope.bases = [];
        $scope.products = [];
        $scope.selectedBase = {};
        $scope.baseTotalPage = 1;
        $scope.baseNowPage = 1;
        $scope.selectedProduct = {};
        $scope.productNowPage = 1;
        $scope.productTotalPage = 1;
        $scope.linkValue = '';
        $scope.viewThumb = '';
        $scope.thumb = '';

        var sliderId = GetQueryString('id');
        $scope.getDetail = function () {
            $http.get(api.getSliderDetail, {
                params: {
                    sliderId: sliderId
                }
            }).success(function (res) {
                if (res.state) {
                    // ...
                    var data = res.data;
                    $scope.title = data.title;
                    $scope.source = data.source;
                    $scope.timepicker = {
                        timestart: data.timestart,
                        timeend: data.timeend
                    };
                    $scope.times = $scope.timepicker.timestart + ' - ' + $scope.timepicker.timeend;
                    $scope.thumb = data.thumb;
                    $scope.viewThumb = data.viewThumb;
                    $scope.type = data.type == 1 ? 'link' : (data.type == 2 ? "base" : "product");
                    if (data.type == 2) {
                        $scope.selectedBase = data.value;
                    } else if (data.type == 3) {
                        $scope.selectedProduct = data.value;
                    }
                } else {
                    ///
                }
            });
        };

        $scope.getDetail();

        $scope.setType = function (type) {
            switch (type){
                case "link":
                    $scope.selectedBase = {};
                    $scope.selectedProduct = {};
                    break;
                case "product":
                    $scope.selectedBase = {};
                    $scope.linkValue = '';
                    break;
                case "base":
                    $scope.selectedProduct = {};
                    $scope.linkValue = '';
                    break;
            }
            $scope.type = type;
        };

        $('#reservationtime').daterangepicker({
            // autoApply: true,
            timePicker24Hour: true,
            opens : 'right',
            locale: {
                format: 'YYYY-MM-DD HH:mm:ss',
                applyLabel : '确定',
                cancelLabel : '取消',
                fromLabel : '起始时间',
                toLabel : '结束时间',
                customRangeLabel : '自定义',
                daysOfWeek : [ '日', '一', '二', '三', '四', '五', '六' ],
                monthNames : [ '一月', '二月', '三月', '四月', '五月', '六月',
                    '七月', '八月', '九月', '十月', '十一月', '十二月' ],
                firstDay : 1
            }
        }, function (start, end, label) {
            $scope.timepicker.timestart = start.format('YYYY-MM-DD HH:mm:ss');
            $scope.timepicker.timeend = end.format('YYYY-MM-DD HH:mm:ss');
            $scope.times = $scope.timepicker.timestart + ' - ' + $scope.timepicker.timeend;
            $scope.$apply();
        });

        $scope.modify = function () {

            var data = {
                id: sliderId,
                type: $scope.type,
                value: '',
                timestart: $scope.timepicker.timestart,
                timeend: $scope.timepicker.timeend,
                title: $scope.title,
                source: $scope.source,
                thumb: $scope.thumb
            };

            // 整理数据
            switch ($scope.type) {
                case "link":
                    data.value = $scope.linkValue;
                    break;
                case "product":
                    data.value = $scope.selectedProduct.id;
                    break;
                case "base":
                    data.value = $scope.selectedBase.id;
                    break;
            }

            if (!data.id || !data.value || !data.title || !data.source || !data.thumb) {
                Modal.alert('请填写完整');
                return false;
            }

            $http.post(api.postData, data).success(function (res) {
                if (res.state) {
                    Modal.success('修改成功');
                } else {
                    var error = !res.error ? '修改失败' : res.error;
                    Modal.error(error);
                }
            });

        };

        $scope.getBase = function (page) {
            $http.get(api.getBaseList, {
                params: {
                    page: !page ? 1 : page
                }
            }).success(function (res) {
                if (res.state) {
                    $scope.bases = res.data.data;
                    $scope.baseTotalPage = res.data.totalPage;
                    $scope.baseNowPage = res.data.nowPage;
                }
            });
        };

        $scope.getProduct = function (page) {
            $http.get(api.getProductList, {
                params: {
                    page: !page ? 1 : page
                }
            }).success(function (res) {
                if (res.state) {
                    $scope.products = res.data.data;
                    $scope.productTotalPage = res.data.totalPage;
                    $scope.productNowPage = res.data.nowPage;
                }
            });
        };

        $scope.productPage = function (page) {
            $scope.getProduct(page);
        };

        $scope.selectProduct = function () {
            $('#selectModalForProduct').modal();
            $scope.getProduct();
        };

        $scope.selectProductItem = function (index) {
            $scope.selectedProduct = angular.copy($scope.products[index]);
            $('#selectModalForProduct').modal('hide');
        };



        // ----- base -----
        $scope.basePage = function (page) {
            $scope.getBase(page);
        };

        $scope.selectBase = function () {
            $('#selectModal').modal();
            $scope.getBase();
        };

        $scope.selectBaseItem = function (index) {
            $scope.selectedBase = angular.copy($scope.bases[index]);
            $('#selectModal').modal('hide');
        };

        function upload() {
            // 初始化Web Uploader
            var uploader = WebUploader.create({
                auto: true,
                server: '/bbs/publish/uploadAttachment?width=645&height=215',
                pick: '#thumbBtn',
                swf: '/assets/library/webuploader/Uploader.swf',
                duplicate: true,
                accept: {
                    title: 'Images',
                    extensions: 'gif,jpg,jpeg,png'
                }
            });

            // 成功
            uploader.on('uploadSuccess', function(file, res) {
                if (res.state) {
                    var data = res.data;
                    $scope.thumb = data.key;
                    $scope.viewThumb = data.thumb;
                } else {
                    var error = res.error ? res.error : '上传失败';
                    Modal.error(error);
                }
            });

            uploader.on('uploadComplete', function( file ) {
                $scope.$apply();
                delete uploader;
                upload();
            });
        }
        upload();

    }]);

})();